package org.jeecg.modules.friends.auth.controller;

import java.util.Objects;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.ControllerMethodCheck;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.friends.auth.dto.AuthDetailInfo;
import org.jeecg.modules.friends.auth.dto.AuthInfo;
import org.jeecg.modules.friends.auth.entity.UserAuthentication;
import org.jeecg.modules.friends.auth.param.EditAuthInfoRequest;
import org.jeecg.modules.friends.auth.service.IUserAuthenticationService;
import org.jeecg.modules.friends.user.entity.User;
import org.jeecg.modules.friends.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

 /**
 * @Description: 认证信息表
 * @Author: jeecg-boot
 * @Date:   2024-05-15
 * @Version: V1.0
 */
@Slf4j
@Api(tags="认证信息表")
@RestController
@RequestMapping("/friends/auth/")
public class UserAuthenticationController extends JeecgController<UserAuthentication, IUserAuthenticationService> {
	@Autowired
	private IUserAuthenticationService userAuthenticationService;
	@Autowired
	private IUserService userService;
	/**
	 * 编辑
	 *
	 * @param request
	 * @return
	 */
	@ApiOperation(value="认证信息编辑", notes="认证信息编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.POST})
	@ControllerMethodCheck
	public Result<?> edit(@RequestBody EditAuthInfoRequest request, @RequestHeader(value = "token") String token) {
		User user = getUser(token);
		if (Objects.isNull(user)) {
			return Result.error("登录过期，请重新登录");
		}
		userAuthenticationService.updateUserAuthInfo(request, user);
		return Result.OK("编辑成功!");
	}
	 @ApiOperation(value="获取认证信息", notes="获取认证信息")
	 @RequestMapping(value = "/queryAuthDetail", method = {RequestMethod.POST})
	 @ControllerMethodCheck
	 public Result<AuthDetailInfo> queryAuthDetail(@RequestHeader(value = "token") String token) {
		 User user = getUser(token);
		 if (Objects.isNull(user)) {
			 return Result.error("登录过期，请重新登录");
		 }
		 AuthDetailInfo authDetailInfo = userAuthenticationService.queryAuthDetailInfo(user);
		 return Result.OK(authDetailInfo);
	 }

	 @ApiOperation(value="认证接口", notes="认证接口")
	 @RequestMapping(value = "/authInfo", method = {RequestMethod.POST})
	 @ControllerMethodCheck
	 public Result<AuthInfo> authInfo (@RequestHeader(value = "token") String token) {
		 User user = getUser(token);
		 if (Objects.isNull(user)) {
			 return Result.error("登录过期，请重新登录");
		 }

		 AuthInfo authInfoDTO = userAuthenticationService.queryUserAuthInfo(user);
		 authInfoDTO.setAuthStatus(user.getAuthStatusV2());
		 return Result.OK(authInfoDTO);
	 }
	 @ControllerMethodCheck
	 @ApiOperation(value="展示邀请页", notes="展示邀请页")
	 @RequestMapping(value = "/showUrl", method = {RequestMethod.POST})
	 public Result<?> showUrl (@RequestHeader(value = "token") String token) {
		 User user = getUser(token);
		 if (Objects.isNull(user)) {
			 return Result.error("登录过期，请重新登录");
		 }

		 userService.closeShowUrl(user);
		 return Result.OK("成功");
	 }

	 private User getUser(String token) {
		 String mobile = JwtUtil.getMobile(token);
		 User user = userService.getUserByPhone(mobile);
		 return user;
	 }

}
